
let oldArrayProto = Array.prototype;

let newArrayProto = Object.create(oldArrayProto)

let methods = ['push', 'pop', 'shift', 'unshift', 'reverse', 'sort', 'splice']

methods.forEach(method => {
    newArrayProto[method] = function (...args) {
        // 先调用老方法
        const result = oldArrayProto[methods].call(this,...args)
        // 对新增的数据做劫持
        let inserted;
        let ob = this.__ob__;
        switch (method) {
            case 'push':
            case 'unshift':
                inserted = args;break;
            case 'splice': // arr.splice(0,1,{a:1}) 1查2删3插
              inserted = args.splice(2);break;
            default: break;
        }
        if (inserted) {
            ob.observeArray(inserted);
        }
        return result;
    }
})

export default newArrayProto;